package com.yeyks.controller.hotel;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.append.sms.SmsService;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.hotel.dal.domain.HotelApply;
import com.yeyks.hotel.param.HotelApplyListParam;
import com.yeyks.hotel.service.HotelApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * 酒店入住申请表 前端控制器
 *
 * @author shuai
 * @date 2019-06-28
 */
@Slf4j
@Api(tags = "酒店入住申请表接口", description = "酒店入住申请表接口")
@RestController
@RequestMapping("/hotelApply")
public class HotelApplyController {
    @Autowired
    private HotelApplyService hotelApplyService;
    @Autowired
    private AccountInfoService accountInfoService;
    @Autowired
    private SmsService smsService;

    /**
     * 酒店申请列表查询
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "酒店申请列表查询")
    @PostMapping("/list")
    public AjaxResponseBody list(@Validated HotelApplyListParam param) {
        if (param.getToken() == null) {
            throw new ServiceException("token不能为空");
        }
        AccountInfo accountInfo = accountInfoService.token2AccountInfo(param.getToken());
        if (AccountType.Administrator != accountInfo.getType()) {
            throw new ServiceException("您无权查看");
        }
        Map<String, Object> map = Maps.newHashMap();
        PageInfo<HotelApply> hotelApplies = hotelApplyService.search(param);
        map.put("pageInfo", hotelApplies);
        map.put("count", hotelApplyService.count(new QueryWrapper<HotelApply>().eq(HotelApply.STATUS, HotelApply.Status.WAIT.value())));
        return AjaxResponseBody.success(map);
    }

    /**
     * 更新酒店申请列表状态
     *
     * @param id
     * @param status
     * @return
     */
    @SystemLog(value = SysLogMsg.HOTEL_APPLY_UPDATE_STATUS)
    @ApiOperation(value = "更新酒店申请列表状态")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "status", value = "状态(0待审核，1已通过，2已拒绝，3已忽略)", required = true, dataType = "int")
    })
    @PostMapping("/update")
    public AjaxResponseBody update(@RequestParam(name = "token") String token,
                                   @RequestParam(name = "id") Integer id,
                                   @RequestParam(name = "status") Integer status) {
        AccountInfo accountInfo = accountInfoService.token2AccountInfo(token);
        if (AccountType.Administrator != accountInfo.getType()) {
            throw new ServiceException("您无权更改");
        }
        if (id == null || status == null) {
            throw new ServiceException("参数不能为空");
        }
        HotelApply byId = hotelApplyService.getById(id);
        if (byId == null) {
            throw new ServiceException("该酒店申请信息不存在");
        }
        if (byId.getStatus().equals(status)) {
            throw new ServiceException("不能修改相同的状态");
        }
        byId.setOperatorId(accountInfo.getId());
        byId.setUpdatedTime(new Date());
        byId.setStatus(status);
        if (hotelApplyService.updateById(byId)) {
            //拒绝的话給用户发送短信
            if (HotelApply.Status.NO_PASS.value().equals(status)) {
                if (!smsService.sendUserToNoPass(byId.getPhone())) {
                    log.error("拒绝入住申请发送短信失败phone = {}", byId.getPhone());
                }
            }
            return AjaxResponseBody.success();
        }
        return AjaxResponseBody.failed("更新状态失败");
    }

}
